<template>
    <div>
        <el-card class="box-card" shadow="never">
            <!--导航栏-->
            <el-page-header @back="back" :content="content">
            </el-page-header>
            <el-form ref="form" class="edit-el-form"  :model="form" :rules="rules" label-width="auto">
                <el-form-item label="项目名称" prop="title">
                    <el-input v-model="form.title" required></el-input>
                </el-form-item>
                <el-form-item label="封面图片" prop="cover_img">
                    <Upload :limit="1" :file_list="cover_img_list" :catch_urls="catch_urls" :remove_urls="remove_urls" :upload_tip="upload_tip"></Upload>
                </el-form-item>
                <el-form-item label="摘要文" prop="summary">
                     <el-input v-model="form.summary"></el-input>
                </el-form-item>
                <el-form-item label="所需人数" prop="need_number">
                     <el-input v-model="form.need_number"></el-input>
                </el-form-item>
                <el-form-item label="开始时间" prop="start_date">
                   <el-date-picker
                         v-model="form.start_date"
                         type="datetime"
                         value-format="yyyy-MM-dd HH:mm:ss"
                         placeholder="选择日期时间">
                   </el-date-picker>
                </el-form-item>
                <el-form-item label="截止时间" prop="end_date">
                   <el-date-picker
                         v-model="form.end_date"
                         type="datetime"
                         value-format="yyyy-MM-dd HH:mm:ss"
                         placeholder="选择日期时间">
                   </el-date-picker>
                </el-form-item>
                <el-form-item label="内容" prop="content">
                    <Editor :catchData="catchData" :child_content="service_content"></Editor>
                </el-form-item>
                <el-form-item label="是否置顶" prop="show_top">
                   <el-switch
                     v-model="form.show_top"
                     active-value='1'
                     inactive-value='0'
                     active-color="#13ce66">
                   </el-switch>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model.number="form.sort">
                      <template slot="append">注：排序越大越靠前，默认100</template>
                    </el-input>
                </el-form-item>
                <el-form-item label="开启报名" prop="open_sign">
                   <el-switch
                     v-model="form.open_sign"
                     active-value='1'
                     inactive-value='0'
                     active-color="#13ce66">
                   </el-switch>
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="form.enabled">
                        <el-radio border label="0">禁用</el-radio>
                        <el-radio border label="1">正常</el-radio>
                    </el-radio-group>
                </el-form-item>

                <!--查看不展示按钮-->
                <el-form-item v-if="edit_type!=0">
                    <el-button type="primary" @click="submitForm('form')" :loading="loading">{{button_name}}</el-button>
                    <el-button @click="back">取消</el-button>
                </el-form-item>
            </el-form>
        </el-card>

    </div>
</template>

<script>
    import Upload from '@/components/Upload.vue';
    import Editor from '@/components/Editor.vue';
    import { getVolunteerInfo,editVolunteer } from '@/api/volunteer.js';
    export default {
        name:'VolunteerEdit',
        components:{ Upload,Editor },
        data() {
            return {
                form: {
                    title: '',
                    cover_img:'',
                    summary:'',
                    need_number:'',
                    start_date:'',
                    end_date:'',
                    content:'',
                    show_top:'0',
                    sort: 100,
                    open_sign:'1',
                    enabled: '1',
                },
                //校验规则
                rules:{
                    title: [
                        { required:true , message:'请输入项目名称',trigger:'blur' }
                    ],
                    cover_img:[
                        { required:true , message:'请上传封面图片',trigger:'blur' }
                    ],
                    start_date:[
                        { type:'string', required:true , message:'请选择开始时间'}
                    ],
                    end_date:[
                        { type:'string', required:true , message:'请选择截止时间'}
                    ],
                    sort:[
                        { required:true , message:'请输入排序号',trigger:'blur' },
                        { type:'number' , message:'排序号必须为数字' }
                    ]
                },
                //加载组件
                loading: false,
                button_name:'立即提交',
                content:'项目添加',
                service_content:'',
                cover_img_list:[],
                image_urls:[],
                upload_tip:'只能上传jpg/png/gif文件，且不超过500kb，仅允许上传一张'
            }
        },
        methods: {
            submitForm(formName){
                let that = this;
                    this.$refs[formName].validate((valid) => {
                            if (valid) {
                                //判断内容不为空
                                if(this.form.content==''){
                                    this.$message({
                                        message:'请输入内容',
                                        type:'error',
                                        duration:1000,
                                    });
                                    return false;
                                };
                                this.loading=true;
                                this.button_name='正在提交';
                                if(that.image_urls.length>0){
                                   that.form.cover_img=that.image_urls[0];
                                }
                                var send_data={
                                    api_token:this.$store.getters.getSessionId,
                                    title:this.form.title,
                                    cover_img:this.form.cover_img,
                                    summary:this.form.summary,
                                    need_number:this.form.need_number,
                                    start_date:this.form.start_date,
                                    end_date:this.form.end_date,
                                    content:this.form.content,
                                    show_top:this.form.show_top,
                                    sort:this.form.sort,
                                    open_sign:this.form.open_sign,
                                    enabled:this.form.enabled,
                                };
                                if(this.id!=-1){
                                    send_data.id=this.id;
                                }
                                editVolunteer(send_data).then(function(response){
                                     let code=response.data.code;
                                     let type=code==1?'success':'error';
                                     let msg=code==1?'成功':'失败';
                                     that.$message({
                                         message:that.content+msg,
                                         duration:1200,
                                         type:type,
                                         onClose:function(e){
                                             that.loading=false;
                                             if(code==1){
                                                that.$router.push('/volunteer/servicelist');
                                             }else{
                                                that.button_name='立即提交';
                                             }
                                         }
                                    })
                                })
                            }else{
                                //处理定位到校验失败处，这边一定要用setTimeout，不用不行
                                setTimeout(()=>{
                                  const is_error=document.getElementsByClassName('is-error');
                                  //button表示图片
                                  is_error[0].querySelector('input,button').focus();
                                },100);
                                return false;
                            }
                    })
            },
            back(){
                this.$router.go(-1);
            },
            catch_urls(url){
              this.image_urls.push(url);
            },
            remove_urls(remove_index){
              this.image_urls.splice(remove_index,1);
            },
            catchData(e){
               this.form.content=e;
            }
        },
        created(){
            if(this.edit_type==1){
                this.content='项目添加';
            }else if(this.edit_type==0){
                this.content='项目详情';
            }else if(this.edit_type==2){
                this.content='项目编辑';
            }
            if(this.id!=-1){
                var send_data={
                    api_token:this.$store.getters.getSessionId,
                    id:this.id
                }
                let that=this;
                getVolunteerInfo(send_data).then(function(response){
                   if(response.data.code==1){
                     let val=response.data.info;
                     that.form.title=val.title;
                     that.form.summary=val.summary;
                     that.form.cover_img=val.cover_img;
                     that.image_urls=[val.cover_img];
                     that.form.need_number=val.need_number;
                     that.form.start_date=val.start_date;
                     that.form.end_date=val.end_date;
                     that.form.content=val.content;
                     that.form.show_top=val.show_top.toString();
                     that.form.sort=val.sort;
                     that.form.open_sign=val.open_sign.toString();
                     that.form.enabled=val.enabled.toString();
                     that.service_content=val.content;
                     that.cover_img_list.push({
                         name:0,
                         url:val.cover_img
                     });
                   }
                })
            }

        },
        computed:{
            id(){
                return this.$route.params.id;
            },
            edit_type(){
                return this.$route.params.edit_type;
            },
        }
    }
</script>

<style>
    .edit-el-form{
        margin-top:30px;
    }
</style>
